#include <tsin/library/TouhouSoloFront.hpp>

#include <tsin/library/Game.hpp>
#include <tsin/library/GameViewport.hpp>
#include <tsin/util/Resolution.hpp>
#include <tsin/display/Display.hpp>
#include <tsin/display/Image.hpp>
#include <tsin/display/DecoratedImage.hpp>

using namespace tsin;

TouhouSoloFront::TouhouSoloFront()
{
}

TouhouSoloFront::~TouhouSoloFront()
{
        delete image;
}

void TouhouSoloFront::init(GameViewport &viewport, StandardResolution res)
{
        this->viewport = &viewport;
        gameRslt = res;
}

void TouhouSoloFront::paint(Display &display, DecoratedImage &image)
{
        StandardResolution imageRslt = image.getResolution();
        static float maxX = Resolution::internalRangeX(gameRslt) / 2;
        static float maxY = Resolution::internalRangeY(gameRslt) / 2;
        int x, y, w, h;       /* for images */
        float fx, fy, fw, fh; /* for display */

        /* The left piece */
        fx = - maxX;
        fy = - maxY;
        fw = viewport->x() + maxX;
        fh = Resolution::internalRangeY(gameRslt);
        x = 0;
        y = (int)((2*maxY - viewport->height()) * RSLT_FACTOR);
        w = (int)(fw * RSLT_FACTOR);
        h = Resolution::resolutionY(gameRslt);
        image.setXRange(x, x + w);
        image.setYRange(y, y + h);
        display.blitImage(image, fx, fy, fw, fh);

        /* The right piece */
        fx += fw + viewport->width();
        fw = maxX - viewport->x()  + viewport->width();
        x += w;
        w = (int)(fw * RSLT_FACTOR);
        image.setXRange(x, x + w);
        image.setYRange(y, y + h);
        display.blitImage(image, fx, fy, fw, fh);

        /* The bottom piece */
        fx = viewport->x();
        fy = - maxY;
        fw = viewport->width();
        fh = viewport->y() + maxY;
        x = 0;
        y = 0;
        w = (int)(fw * RSLT_FACTOR);
        h = (int)(fh * RSLT_FACTOR);
        image.setXRange(x, x + w);
        image.setYRange(y, y + h);
        display.blitImage(image, fx, fy, fw, fh);

        /* The top piece */
        fy += fh + viewport->height();
        fh = maxY - viewport->y() + viewport->height();
        y += h;
        h = (int)(fh * RSLT_FACTOR);
        image.setXRange(x, x + w);
        image.setYRange(y, y + h);
        display.blitImage(image, fx, fy, fw, fh);
}